/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Base;
using Shuriken.Core.Store;

namespace Shuriken.Core.UI.StandardUI
{
    /// <summary>
    /// Action Display Slot 
    /// </summary>
    class ActionItemController : BaseItemController 
    {
        private Shuriken.Core.Store.SortedDisplayItemList m_InitialActionItems = null;
 
        /// <summary>
        /// Class to represent the action 
        /// </summary>
        /// <param name="ParentForm">Parent Form</param>
        /// <param name="UIControl">UI Control representing the action.</param>
        public ActionItemController(frmMain ParentForm, InputItemView UIControl)
            : base(ParentForm, UIControl)
        {
            UIControl.SearchTerm = "Action";
        }

        /// <summary>
        /// Loads a new list of actions for this subject
        /// </summary>
        /// <param name="newSubject">Subject</param>
        internal void LoadActionsForSubject(Shuriken.Base.ISubject subject)
        {
            Shuriken.Core.Store.ActionList actions = new Shuriken.Core.Store.ActionList();
            actions.AddRange(subject.GetListOfActions());
            
            // Erasing the hit mask for each action
            foreach (Shuriken.Core.Store.ISearchable item in actions)
            {
                item.HitMask = new Shuriken.Core.Ranking.MutableIndexSet();
            }

            m_InitialActionItems = (Shuriken.Core.Store.SortedDisplayItemList)actions; 

            System.Diagnostics.Trace.Assert((actions.DefaultActionIndex != -1), "No Default Action for subject!");
            System.Diagnostics.Debug.Assert(actions.Count != 0, "Empty Action list is not allowed!");

            // m_ParentForm.ActionSlot.UpdateDisplaySlot(string.Empty, actions.DefaultActionIndex, new Shuriken.Core.Ranking.MutableIndexSet(), actions);
            this.UpdateDisplaySlot(string.Empty, actions.DefaultActionIndex, actions);
        }

        /// <summary>
        /// The initial display item list for this slot. 
        /// </summary>
        public override Shuriken.Core.Store.SortedDisplayItemList InitialDisplayItems
        {
            get
            {
                return m_InitialActionItems; 
            }
        }

        /// <summary>
        /// Erasing display content
        /// </summary>
        /// <remarks>
        /// Reseting back the original to the state of the actions
        /// </remarks>
        public override bool BackKeyPressed()
        {
            Shuriken.Core.Store.ActionList actions = (Shuriken.Core.Store.ActionList)this.InitialDisplayItems;

            // Erasing the hitmask for all action items
            foreach (Shuriken.Core.Store.ISearchable item in actions)
            {
                item.HitMask = new Shuriken.Core.Ranking.MutableIndexSet();
            }

            // Change: let's keep the selected item instead of the default action 
            // this.UpdateDisplaySlot(string.Empty, actions.DefaultActionIndex, actions);
            this.UpdateDisplaySlot(string.Empty, SelectedItemIndex, actions);
            m_ParentForm.HideItemList();

            return true; 
        }

        /// <summary>
        /// Decides if tab is possible from this UI slot. 
        /// </summary>
        /// <returns>true if the next TAB is possible</returns>
        protected override bool TABPossible()
        {
            IAction action = (IAction)this.SelectedDisplayItem;

            if (action == null)
            {
                return false;
            }

            return action.IsAttributed; 
        }

        /// <summary>
        /// Selected item is changed. 
        /// </summary>
        /// <param name="NewIndex">New selected item.</param>
        /// <remarks>
        /// This override is needed here mainly for handling attrbiuted actions. This method is responsible to resize the 
        /// main form in case if it has an attrbiute and shrink it if it has not
        /// </remarks>
        protected override void ChangeSelectedItem(int NewIndex)
        {
            base.ChangeSelectedItem(NewIndex);

            if (TABPossible())
            {
                // TAB is possible, so expand the main form so it can
                // accomodate one more slot
                this.m_ParentForm.ExpandUIBy(0, m_InputPanel.Height);
            }
            else
            {
                // Action has no attribute, so we need to hide the slot 
                // and shrink to the original size. 
                this.m_ParentForm.AttributeSlot.Visible = false;
                this.m_ParentForm.ShrinkUIToOriginal();
            }
            
        }
    }
}
